//
//  PpFeedBackVC.swift
//  Palmpilot
//
//  Created by 杨伟毅 on 2018/10/23.
//  Copyright © 2018年 杨伟毅. All rights reserved.
//

import UIKit
import QorumLogs
import MBProgressHUD

class PpFeedBackVC: PpBaseViewController {
    let mbp = MBProgressHUD()
    
    @IBOutlet weak var basePhone: UITextField!
    
    @IBOutlet weak var contentTextView: UITextView!
    @IBOutlet weak var postButton: UIButton!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.title = PpLanguageSwitchTool.getString(key: "Feedback")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(mbp)
        // Do any additional setup after loading the view.
    }
    
    @IBAction func postButtonClick(_ sender: UIButton) {
        guard let value = basePhone.text,value.isEmpty == false else {
            showAlertStr(str: PpLanguageSwitchTool.getString(key: "phoneTips"))
           return
        }
        guard let contenStr = contentTextView.text,contenStr.isEmpty == false else {
            showAlertStr(str: PpLanguageSwitchTool.getString(key: "contentTips"))
            return
        }
        weak var weakSelf = self
        mbp.label.text  = PpLanguageSwitchTool.getString(key: "postFeedTips")
        mbp.show(animated: true)
        PpBaseLancloudTool.postDataLean(content: contenStr, emailStr: value) { (data, respone, error) in
            guard let data = data,let json = try? JSONSerialization.jsonObject(with: data, options: []) as? Dictionary<String,Any> else {
                return
            }
            if let strongSelf = weakSelf{
                strongSelf.mbp.label.text = "提交成功"
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                    strongSelf.mbp.hide(animated: true)
                    strongSelf.basePhone.resignFirstResponder()
                    strongSelf.contentTextView.resignFirstResponder()
                })
            }
            }
    }
    func showAlertStr(str:String) -> Void {
        let showAlertVC = UIAlertController.init(title: PpLanguageSwitchTool.getString(key: "alertTitle"), message: str, preferredStyle: .alert)
        let sureAction = UIAlertAction.init(title:PpLanguageSwitchTool.getString(key: "sure"), style: .cancel) { (action) in
        }
        showAlertVC.addAction(sureAction)
        self.present(showAlertVC, animated: true, completion: nil)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        basePhone.resignFirstResponder()
        contentTextView.resignFirstResponder()
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
